package net.vkits.h5;

import net.vkits.anjiamiao.application.ComplainApplicationService;
import net.vkits.anjiamiao.domain.model.Message;
import net.vkits.anjiamiao.domain.repository.MessageRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.time.LocalDate;

/**
 * Created by ethan-liu on 2016/12/29.
 */
@Controller
@RequestMapping("/h5")
public class H5Controller {
    private static final Logger logger = LoggerFactory.getLogger(H5Controller.class);

    @Resource
    private ComplainApplicationService complainApplicationService;
    @Resource
    private MessageRepository messageRepository;

    @RequestMapping(value ="/appeal/{cellPhoneNum}", method = RequestMethod.GET)
    public ModelAndView appeal(@PathVariable String cellPhoneNum) {
        logger.info("cellPhoneNum: {}", cellPhoneNum);
        ModelMap modelMap = new ModelMap();
        modelMap.put("cellPhoneNum",cellPhoneNum);
        return new ModelAndView("/appeal", modelMap);
    }

    @RequestMapping(value ="/appeal", method = RequestMethod.POST)
    public ModelAndView appealAccept(@RequestParam(value = "appeal", required = true) String appeal,
                               @RequestParam(value = "cellPhoneNum", required = true) String cellPhoneNum) {
        complainApplicationService.submitComplain(appeal, cellPhoneNum);
        ModelMap modelMap = new ModelMap();
        modelMap.put("startDate", LocalDate.now());
        modelMap.put("endDate", LocalDate.now().plusDays(1));
        return new ModelAndView("/appeal_accept", modelMap);
    }

    @RequestMapping(value = "/appeals/{id}", method = RequestMethod.GET)
    public ModelAndView appealStatus(@PathVariable String id) {
        ModelMap modelMap = new ModelMap();
        Message message = messageRepository.findOne(Long.parseLong(id));
        logger.info("message is: {}", message);
        modelMap.put("message", message);
        return new ModelAndView("/appeals_id", modelMap);
    }

    @RequestMapping(value ="/orderStatus/{id}", method = RequestMethod.GET)
    public ModelAndView orderStatus(@PathVariable String id) {
        ModelMap modelMap = new ModelMap();
        Message message = messageRepository.findOne(Long.parseLong(id));
        modelMap.put("message", message);
        return new ModelAndView("/orderStatus", modelMap);
    }
}
